//
//  FindPwdViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/11.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "FindPwdViewController.h"
#import "ResetPwdViewController.h"
#import "CountryCodeViewController.h"

@interface FindPwdViewController ()<UITextFieldDelegate>
{
//    UIButton *countryCodeBtn;
    UITextField *phoneTextField;
    
    
    NSString *countryCodeStr;
}
@end

@implementation FindPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    countryCodeStr = @"86";
    countryCodeStr = @"";
    [self initUI];
}
- (void)countryCodeBtnTap {
    CountryCodeViewController *CCVC = [[CountryCodeViewController alloc] init];
    CCVC.selectCode = ^(NSString * _Nonnull code) {
        self->countryCodeStr = code;
//        [self->countryCodeBtn setTitle:[NSString stringWithFormat:@"+ %@", code] forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:CCVC animated:YES];
}
- (void)initUI {
    self.navigationItem.title = @"找回密码";
    
//    countryCodeBtn = [UIToolsControl initButtonWithFrame:CGRectMake(ScalePS(20), ScalePS(25), ScalePS(50), ScalePS(50)) imageName:nil target:self action:@selector(countryCodeBtnTap) title:[NSString stringWithFormat:@"+ %@", countryCodeStr] titleColor:THEMECOLOR_OF_TEXT_333333];
//    countryCodeBtn.titleLabel.font = Font(Font_15);
//    countryCodeBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
//    countryCodeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    [self.view addSubview:countryCodeBtn];
    
//    phoneTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(EndX(countryCodeBtn)+10*Scale, 25*Scale, WIDTH-60*Scale-ScalePS(50), 50*Scale) placeholder:PhoneNumberPlaceholderText secureTextEntry:NO font:Font_17 padding:YES];
    phoneTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(30*Scale, 25*Scale, WIDTH-60*Scale, 50*Scale) placeholder:PhoneNumberPlaceholderText secureTextEntry:NO font:Font_17 padding:YES];
    [phoneTextField borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    phoneTextField.delegate = self;
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phoneTextField];
    
    UIButton *nextBtn = [UIToolsControl initButtonWithFrame:CGRectMake(40*Scale, 230*Scale, WIDTH-80*Scale, 46*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"下一步" titleColor:COLOR_OF_WHITE];
    nextBtn.titleLabel.font = FontBold(Font_17);
    nextBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    nextBtn.layer.cornerRadius = 23*Scale;
    nextBtn.layer.masksToBounds = YES;
    [self.view addSubview:nextBtn];
}
- (void)okBtnTap {
    if (phoneTextField.text.length>0) {
        if ([UIToolsControl validatePhoneNumber:phoneTextField.text]) {
            SVPHUDShow;
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
            dic = [UIToolsControl getPublicParameter];
            [dic setObject:phoneTextField.text forKey:@"mobile"];
            [dic setObject:countryCodeStr forKey:@"country_code"];
            
            [BaseNetManager requestWithMethod:POST path:Check_PhoneNumber_For_ResetPwd_URL params:dic successBlock:^(NSDictionary *responseObject) {
                if ([responseObject[@"code"] integerValue] == 2000) {
                    SVPHUDSuccess(responseObject[@"msg"]);
                    ResetPwdViewController *RPVC = [[ResetPwdViewController alloc] init];
                    RPVC.phoneNumber = self->phoneTextField.text;
                    [self.navigationController pushViewController:RPVC animated:YES];
                } else {
                    SVPHUDError(responseObject[@"msg"]);
                }
            } failurBlock:^(NSError *error) {
                SVPHUDError(NetErrorAlertText);
            }];
        } else {
            SVPHUDError(PhoneNumberOKAlertText);
            [phoneTextField becomeFirstResponder];
        }
    } else {
        SVPHUDError(PhoneNumberAlertText);
        [phoneTextField becomeFirstResponder];
    }
}

@end
